package com.rc.saas.mini.service.sms;

import com.rc.saas.mini.mapper.sms.TenantSmsMapper;
import com.rc.saas.mini.mapper.sms.TenantSmsRecordMapper;
import com.rc.saas.mini.model.sms.TenantSmsRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TenantSmsServiceImpl implements TenantSmsService {

    @Autowired
    private TenantSmsMapper tenantSmsMapper;

    @Autowired
    private TenantSmsRecordMapper tenantSmsRecordMapper;

    @Override
    public TenantSmsMapper getMapper() {
        return tenantSmsMapper;
    }

    @Override
    public void  updateTenantSmsByTenantCode(String tenantCode, Long enterpriseId, Long enterpriseMemberId, Long memberId,
                                                    String mobile, String recordType, String content,String code, String remark){
        //更新表TenantSms 剩余量
        tenantSmsMapper.updateByTenantCode(tenantCode);
        //更新表TenantSmsRecord
        TenantSmsRecord tenantSmsRecord = new TenantSmsRecord();
        tenantSmsRecord.setTenantCode(tenantCode);
        tenantSmsRecord.setEnterpriseId(enterpriseId);
        tenantSmsRecord.setEnterpriseMemberId(enterpriseMemberId);
        tenantSmsRecord.setMemberId(memberId);
        tenantSmsRecord.setMobile(mobile);
        tenantSmsRecord.setRecordType(recordType);
        if (code == null) {
            code = "";
        }
        tenantSmsRecord.setContent(content.length() > 0 ? content.replaceAll("code", code) : code);
        tenantSmsRecord.setRemark(remark != null ? remark : tenantSmsRecord.getContent());
        tenantSmsRecord.setCreatedTime(new Date());
        tenantSmsRecord.setLastUpdatedTime(new Date());
        tenantSmsRecordMapper.insertSelective(tenantSmsRecord);
    }


}
